package org.robot;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class EchoClient {
	static Bootstrap b = new Bootstrap();
	static EventLoopGroup group = new NioEventLoopGroup();
	static {
		b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true)
				.handler(new ChannelInitializer<SocketChannel>() {
					protected void initChannel(SocketChannel ch) throws Exception {
						ch.pipeline().addLast("codec", new EchoCodec());
						ch.pipeline().addLast("handler", new EchoHandler());
					}
				});
	}

	public static ChannelFuture connect(String host, int port) throws InterruptedException {
		return b.connect(host, port).sync();
	}
}
